package com.yuntu.util;

import java.sql.*;

/**
 * @author MrWang-shuizhishuiheshui
 * @description
 */
public class TestCon {
    public static void main(String[] args) {
        Connection con=null;
        try {
            //1.加载驱动
            // com.mysql.jdbc.Driver(mysql5的驱动名)
            // com.mysql.cj.jdbc.Driver (mysql8的驱动名)
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.建立连接
            con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/myschool?" +
                    "characterEncoding=utf8&useSSL=false&serverTimezone=UTC", "root", "root");
            //3.获取Statement对象
            Statement conStatement = con.createStatement();
            //4.由conStatement执行sql语句
            ResultSet rs = conStatement.executeQuery("select *  from grade");
            //5.循环取出rs里面的内容
            while(rs.next()){
                System.out.println("编号:"+rs.getInt("id"));
                System.out.println("年级:"+rs.getString("gradename"));
            }
            System.out.println("连接成功"+con);
        } catch (ClassNotFoundException e) {
            System.out.println("架包加载有问题");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("连接加载有问题");
            e.printStackTrace();
        } finally {
            //释放连接资源
            if (con!=null){
                try {
                    con.close();
                    System.out.println("连接关闭");
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
